package com.liu.communitysystemend.module.notice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.notice.domain.Notice;
import com.liu.communitysystemend.module.notice.mapper.NoticeMapper;
import com.liu.communitysystemend.module.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
*/
@Service
public class NoticeServiceImpl implements NoticeService {
    
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public Page<Notice> getPage(Notice notice) {
        LambdaQueryWrapper<Notice> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getIsDeleted,0);
        Page<Notice> page=new Page<>(notice.getPage().getPage(),notice.getPage().getSize());
        Page<Notice> poiPage=(Page<Notice>) noticeMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Notice add(Notice notice) {
        if(notice ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=noticeMapper.insert(notice);
        if(insert>0){
            return notice;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Integer deleteById(String id) {
        Notice notice = selectOneById(id);
        if(notice==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=noticeMapper.deleteById(id);
        return i;
    }

    @Override
    public Notice update(Notice notice) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getId,notice.getId());
        int update = noticeMapper.update(notice, wrapper);
        if (update>0){
            return notice;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public Notice getNoticeById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getId,id);
        Notice notice = noticeMapper.selectOne(wrapper);
        return notice;
    }
    public Notice selectOneById(String id){
        Notice notice=noticeMapper.selectById(id);
        return  notice;
    }
}
